package com.app.controller;


import com.app.entity.AppCategory;
import com.app.entity.AppInfo;
import com.app.entity.DataDictionary;
import com.app.entity.bean.AppInfoCondition;
import com.app.entity.bean.Page;
import com.app.service.CommonService;
import com.app.service.DevInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * 开发者控制器
 */
@Controller
@RequestMapping("/developer")
public class DevController {
    @Autowired
    private DevInfoService devInfoService;
    @Autowired
    private CommonService commonService;
    @RequestMapping("/main")
    public String main(){
        return "developer/main";
    }
    @RequestMapping("/appinfolist")
    public String list(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "querySoftwareName", required = false) String softwareName,
                       @RequestParam(value = "queryFlatformId", required = false) Long flatformId,
                       @RequestParam(value = "queryCategoryLevel1", required = false) Long categoryLevel1,
                       @RequestParam(value = "queryCategoryLevel2", required = false) Long categoryLevel2,
                       @RequestParam(value = "queryCategoryLevel3", required = false) Long categoryLevel3,
                       Model model){
        //封装查询条件
        AppInfoCondition condition = new AppInfoCondition(softwareName, flatformId,
                categoryLevel1, categoryLevel2, categoryLevel3, pageIndex, 5);
        //根据查询条件查询分页的app信息
        Page<AppInfo> aPage = devInfoService.getPageApp(condition);
        //查询所有平台信息
        List<AppCategory> oCategoryList = commonService.getOneCategoryList();
        List<DataDictionary> flatformList = commonService.getFlatformList();
        List<DataDictionary> dataTypeCode = devInfoService.getDataTypeCode();
        model.addAttribute("aPage", aPage);
        model.addAttribute("dataTypeCode", dataTypeCode);
        model.addAttribute("oCategoryList", oCategoryList);
        model.addAttribute("flatformList", flatformList);

        //回显查询条件
        if (softwareName != null && softwareName.length() != 0) {
            model.addAttribute("querySoftwareName", softwareName);
        }
        if (flatformId != null && flatformId != 0) {
            model.addAttribute("queryFlatformId", flatformId);
        }
        //如果查询条件中有一级分类，则同时还要查询其下的所有二级分类
        if (categoryLevel1 != null && categoryLevel1 != 0) {
            model.addAttribute("queryCategoryLevel1", categoryLevel1);
            //查询所有二级分类
            List<AppCategory> twoCategoryList = commonService.getCategoryListByParentId(categoryLevel1);
            model.addAttribute("twoCategoryList", twoCategoryList);
        }
        //如果查询条件中有二级分类，则同时还要查询其下的所有三级分类
        if (categoryLevel2 != null && categoryLevel2 != 0) {
            model.addAttribute("queryCategoryLevel2", categoryLevel2);
            //查询所有三级分类
            List<AppCategory> threeCategoryList = commonService.getCategoryListByParentId(categoryLevel2);
            model.addAttribute("threeCategoryList", threeCategoryList);
        }
        if (categoryLevel3 != null && categoryLevel3 != 0) {
            model.addAttribute("queryCategoryLevel3", categoryLevel3);
        }

        return "developer/appinfolist";
    }
}
